/**
 * 
 */
package jp.sourceforge.akjrcp.dropsomething2.psp;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;

import java.util.HashMap;
import java.util.Map;

import jp.sourceforge.akjrcp.akj_betas.Messages;
import jp.sourceforge.akjrcp.commons.ui.FileDirChoicer2;
import jp.sourceforge.akjrcp.commons.ui.widget.LayoutBuilder;
import jp.sourceforge.akjrcp.commons.ui.widget.LayoutDataBuilder;
import jp.sourceforge.akjrcp.commons.ui.widget.WidgetBuilder;
import jp.sourceforge.akjrcp.commons.util.FileNameUtils;
import jp.sourceforge.akjrcp.dropsomething2.AbstractDropFileGenericParametersOptionEditor;
import jp.sourceforge.akjrcp.dropsomething2.DropFileGenericParameterSetting;
import jp.sourceforge.akjrcp.dropsomething2.psp.PSPConvertTabBuilder2.PSPConvertTab2;
import jp.sourceforge.akjrcp.dropsomething2.psp.PSPConvertTabBuilder2.SetPSPDir;
import jp.sourceforge.akjrcp.ffmpeg.FFMpegOptions;
import jp.sourceforge.akjrcp.ui.GenericParametersEventListener;
import jp.sourceforge.akjrcp.ui.GenericParametersFFMpegPreference;
import jp.sourceforge.akjrcp.ui.param.BitrateChangeButton;
import jp.sourceforge.akjrcp.ui.param.ParamBoolean;
import jp.sourceforge.akjrcp.ui.param.ParamCombo;
import jp.sourceforge.akjrcp.ui.param.ParamFile;
import jp.sourceforge.akjrcp.ui.param.ParamHidden;
import jp.sourceforge.akjrcp.ui.param.ParamItem;

public class PSPOptionEditor extends AbstractDropFileGenericParametersOptionEditor implements GenericParametersEventListener{

			Map parameterMap=new HashMap();
			private Composite inputComposite;
			
			private boolean newMode=false;
			private boolean dirMode;
			private PSPConvertTab2 pspConvertTab2;
			private Composite basicComposite;
			public synchronized Map getParameters(){
				return parameterMap;
			}
			public  void updateParameters(){
				Map parameterMap=new HashMap();
				for (int i = 0; i < paramList.size(); i++) {
					ParamItem item=paramList.get(i);
					if(item.getValue().length()>0){
						parameterMap.put(item.getKey(), item.getValue());
					}
				}
				synchronized(this){
				this.parameterMap=parameterMap;
				}
			}
			
			
			public void setParameters(Map map){
				for (int i = 0; i < paramList.size(); i++) {		
					ParamItem item=paramList.get(i);
					String key=item.getKey();
					//ignore when mode is new and about dir params
					if(!isIgnoreEditModeKey(key) || !isNewMode()){
					String value=(String) map.get(key);
					if(value!=null){
						//log.info(item.getKey()+","+value);
						item.setValue(value);
					}else{
						item.setValue("");
					}
					}
				}
				sameFolderSelected();//for dir info
				updateStatus();//convert test
			}
			
			private boolean isIgnoreEditModeKey(String key){
				String vs[]={DropFileGenericParameterSetting.BACKUP,
				DropFileGenericParameterSetting.EXPORT_DIR,
				DropFileGenericParameterSetting.INPUT_AUDIO1,
				DropFileGenericParameterSetting.INPUT_VIDEO1,
				DropFileGenericParameterSetting.SAME_DIRECTORY,
				DropFileGenericParameterSetting.USE_AVISYNTH
				};
				for (int i = 0; i < vs.length; i++) {
					if(vs[i].equals(key)){
						return true;
					}
				}
				return false;
			}
			
			//for overwrite
			public Composite createOtherControls(Composite parent) {
				return null;
			}
			
			private Object alignCenterGrid() {
				GridData gtop=new GridData();
				gtop.horizontalAlignment=SWT.CENTER;
				return gtop;
			}
			
			public Composite createControls(Composite root) {
				
				createDirectoryInfo(root);
				
				directoryChooser.getFileDirChoicer().addSelectableFileName(FileNameUtils.getMyVideoPath());
				directoryChooser.getFileDirChoicer().addSelectableFileName(FileNameUtils.getMyDocumentsPath());
				directoryChooser.getFileDirChoicer().addSelectableFileName(FileNameUtils.getDesktopPath());
				directoryChooser.getFileDirChoicer().addSelectableFileName(FileNameUtils.getMyMusicPath());
				directoryChooser.getFileDirChoicer().addSelectableFileName(FileNameUtils.getMyPicturesPath());
				Composite pspDirectoryPanel=new Composite(root,SWT.NULL);
				pspDirectoryPanel.setLayout(new GridLayout(2,false));
				Button pspFolder=new Button(pspDirectoryPanel,SWT.PUSH);
				pspFolder.setText(Messages.getString("pspcovert.exportpsp"));
				pspFolder.addSelectionListener(new SetPSPDir(pspConvertTab2));
				
				//TODO avisynth move up to PSP IO
				avisynth = new ParamBoolean(pspDirectoryPanel, SWT.CHECK,DropFileGenericParameterSetting.USE_AVISYNTH,Messages.getString("pspconvert.use_avisynth"),false);
				avisynth.setToolTipText(Messages
						.getString("pspconvert.use_avisynth_tooltip"));
				//TODO add save system like dir
				//avisynth.getButton().addSelectionListener(savePreference);
				
				if(!dirMode){
					directoryInfoComposite.setVisible(false);
					pspDirectoryPanel.setVisible(false);
					avisynth.setVisible(false);
				}
				
				
				//avisynth.setBoolean(preferenceStore.getBoolean(getPreferenceKey()+".directoryinfo.samefolder"));
				
				//TODO restore
				//restoreControl(avisynth);
				if(newMode){
				createDropFileComposite(root);
				}
				Composite parent=WidgetBuilder.createTwoGridLayoutComposite(root, SWT.NULL);
				parent.setLayoutData(LayoutDataBuilder.fillBothGridData());
				
				CTabFolder tab = new CTabFolder(parent, SWT.BOTTOM|SWT.FLAT);
				tab.setLayoutData(new GridData(GridData.FILL_VERTICAL));
				basicComposite = new Composite(tab,SWT.NONE);
				basicComposite.setLayout(LayoutBuilder.makeGridLayoutNoMargin(1, false));
				
				CTabItem tabItem1 = new CTabItem(tab, SWT.NONE);
				tabItem1.setText(Messages.getString("ffmpegconvert.tab_basic"));
				
				tabItem1.setControl(basicComposite);
				
				//createOtherControls(basicComposite);
				
				
		
			
				
				
				Group vgroup=new Group(basicComposite,SWT.NULL);
				
				vgroup.setLayout(LayoutBuilder.makeGridLayoutNoMargin(2, false));
				vgroup.setText(Messages.getString("ffmpegconvert.Video"));
				GridData vdata=new GridData();
				vdata.minimumHeight=120;
				vdata.grabExcessVerticalSpace=false;
				vgroup.setLayoutData(vdata);
				
				//mp4 fixed
				ParamHidden extension=new ParamHidden("EXTENSION","mp4");
				addParamList(extension);
				
				ParamCombo vcodec= new ParamCombo(vgroup,SWT.NONE,"vcodec",Messages.getString("ffmpegconvert.vcodec"),
						new String[]{"h264","xvid"});
				vcodec.setToolTipText(Messages.getString("ffmpegconvert.vcodec_tooltip"));
				addParamList(vcodec);
				
				ParamCombo maxfr= new ParamCombo(vgroup,SWT.NONE,GenericParametersFFMpegPreference.MAX_FRAME_RATE,Messages.getString("ffmpegconvert.maxframerate"),
						new String[]{"","30","12","14.895","15","24","25","29.97"});
				maxfr.setToolTipText(Messages.getString("ffmpegconvert.maxframerate_tooltip"));
				addParamList(maxfr);
				
				
			
				
				//TODO add psp sizes
				ParamCombo s= new ParamCombo(vgroup,SWT.NONE,"s",Messages.getString("ffmpegconvert.size"),
						new String[]{"","160x112","320x240","368x272","192x112","368x208","480x272","640x480","720x480","352x480","480x270","360x270"});

				s.setToolTipText(Messages.getString("ffmpegconvert.size_tooltip"));
				addParamList(s);
				
				ParamCombo r= new ParamCombo(vgroup,SWT.NONE,"r",Messages.getString("ffmpegconvert.rate"),
						new String[]{"","1","12","14.895","15","24","25","29.97","30","ntsc","pal"});
				r.setToolTipText(Messages.getString("ffmpegconvert.rate_tooltip"));
				addParamList(r);
				
				ParamCombo b= new ParamCombo(vgroup,SWT.NONE,"b",Messages.getString("ffmpegconvert.bitrate"),
						new String[]{"128k","192k","256k","384k","448k","512k","576k","628k","700k","768k","896k","1024k","1208k","1536k","1792k","2048k","2560","3072k","5000k","7500k","9216k","10000k","50000k","100000k"});
				b.setToolTipText(Messages.getString("ffmpegconvert.bitrate_tooltip"));
				addParamList(b);
				
				BitrateChangeButton bitrateControler=new BitrateChangeButton(vgroup,32,b);
				
				
				
				Composite dummy=new Composite(vgroup,SWT.NULL);
				
				ParamCombo aspect= new ParamCombo(vgroup,SWT.NONE,"aspect",Messages.getString("ffmpegconvert.aspect"),
						new String[]{"","4:3","16:9"});
				aspect.setToolTipText(Messages.getString("ffmpegconvert.aspect_tooltip"));
				addParamList(aspect);
				
			
			
		
				
				
			
				Group v2group=new Group(basicComposite,SWT.NULL);
				v2group.setLayout(LayoutBuilder.makeGridLayoutNoMargin(3, false));
				
				ParamBoolean twopass= new ParamBoolean(v2group,SWT.NONE,GenericParametersFFMpegPreference.TWO_PASS_ENCODE,Messages.getString("ffmpegconvert.twopassencode"));
				twopass.setToolTipText(Messages.getString("ffmpegconvert.twopassencode_tooltip"));
				addParamList(twopass);
				
				
				ParamBoolean deinterlace= new ParamBoolean(v2group,SWT.NONE,"deinterlace",Messages.getString("ffmpegconvert.deinterlace"));
				deinterlace.setToolTipText(Messages.getString("ffmpegconvert.deinterlace_tooltip"));
				addParamList(deinterlace);
				
				ParamBoolean hq= new ParamBoolean(v2group,SWT.NONE,PSPConvertTabBuilder2.PSP_HQ,Messages.getString("pspconvert.hq"));
				hq.setToolTipText(Messages.getString("pspconvert.hq_tooltip"));
				addParamList(hq);
				
				
				
				Group agroup=new Group(basicComposite,SWT.NULL);
				agroup.setText(Messages.getString("ffmpegconvert.Audio"));
				agroup.setLayout(LayoutBuilder.makeGridLayoutNoMargin(2, false));
				GridData adata=new GridData();
				adata.minimumHeight=120;
				adata.grabExcessVerticalSpace=false;
				agroup.setLayoutData(adata);
				
				
				
				ParamHidden acodec=new ParamHidden("acodec","aac");
				addParamList(acodec);
				
				
				ParamCombo ab= new ParamCombo(agroup,SWT.NONE,"ab",Messages.getString("ffmpegconvert.ab"),
						new String[]{"32k","64k","96k","128k","160k","192k","256k","320k","360k","384k"});
				ab.setToolTipText(Messages.getString("ffmpegconvert.ab_tooltip"));
				addParamList(ab);
				
				ParamCombo ar= new ParamCombo(agroup,SWT.NONE,"ar",Messages.getString("ffmpegconvert.ar"),
						new String[]{"","8000","16000","22050","24000","32000","44100","48000"});
				ar.setToolTipText(Messages.getString("ffmpegconvert.ar_tooltip"));
				addParamList(ar);
				
				ParamCombo ac= new ParamCombo(agroup,SWT.NONE,"ac",Messages.getString("ffmpegconvert.ac"),
						new String[]{"","2","1"});
				ac.setToolTipText(Messages.getString("ffmpegconvert.ac_tooltip"));
				addParamList(ac);
				
				ParamCombo vol= new ParamCombo(agroup,SWT.NONE,"vol",Messages.getString("ffmpegconvert.vol"),
						new String[]{"","64","128","224","256","272","288","304","320","352","384","512","640","768","896","1024","2048"});
						
						
						
						
				vol.setToolTipText(Messages.getString("ffmpegconvert.vol_tooltip"));
				addParamList(vol);
				
				inputComposite = new Composite(tab,SWT.NONE);
				inputComposite.setLayout(new GridLayout(1,false));
				CTabItem tabItem4 = new CTabItem(tab, SWT.NONE);
				tabItem4.setText(Messages.getString("ffmpegconvert.tab_input"));
				
				tabItem4.setControl(inputComposite);
				Label input_d=WidgetBuilder.createLabel(inputComposite, Messages.getString("ffmpegconvert.tab_input_description"));
				
				
				ParamFile inputVideo=new ParamFile(inputComposite,SWT.NULL,DropFileGenericParameterSetting.INPUT_VIDEO1,Messages.getString("ffmpegconvert.input_video"),FileDirChoicer2.FILE,true);
				inputVideo.setLayoutData(LayoutDataBuilder.fillHorizontalGridData());
				addParamList(inputVideo);
				ParamFile inputAudio=new ParamFile(inputComposite,SWT.NULL,DropFileGenericParameterSetting.INPUT_AUDIO1,Messages.getString("ffmpegconvert.input_audio"),FileDirChoicer2.FILE,true);
				inputAudio.setLayoutData(LayoutDataBuilder.fillHorizontalGridData());
				addParamList(inputAudio);
				
				ParamBoolean shortest=new ParamBoolean(inputComposite,SWT.NULL,FFMpegOptions.OPTION_SHORTEST,Messages.getString("ffmpegconvert.shortest"),false);
				addParamList(shortest);
				tab.setSelection(0);
				tab.forceFocus();//must for focus tab.
				
				if(!dirMode){
					inputComposite.setVisible(false);
					inputVideo.setVisible(false);
					inputAudio.setVisible(false);
					shortest.setVisible(false);
				}
				//important for event
				this.addListener(this);
				
				return parent;//for left side
			}
			public void updateModified(boolean modified) {
				updateParameters();
			}
			public Composite getInputComposite() {
				return inputComposite;
			}
			public boolean isNewMode() {
				return newMode;
			}
			public void setNewMode(boolean newMode) {
				this.newMode = newMode;
			}
			
			/*
			 * should i fix it?
			 * (�� Javadoc)
			 * @see jp.sourceforge.akjrcp.ui.AbstractGenericParametersOptionEditor#updateStatus()
			 */
			@Override
			public void updateStatus() {
				if(pspConvertTab2!=null){
				pspConvertTab2.updateStatus();
				}else{
				//maybe pspoptiondialog call
				}
			}
			
			
			@Override
			public ParamItem getItem(String key) {
				for (int i = 0; i < paramList.size(); i++) {		
					ParamItem item=paramList.get(i);
					if(item.getKey().equals(key)){
						return item;
					}
				}
				return null;
			}
			public PSPConvertTab2 getPspConvertTab2() {
				return pspConvertTab2;
			}
			public void setPspConvertTab2(PSPConvertTab2 pspConvertTab2) {
				this.pspConvertTab2 = pspConvertTab2;
			}
			public void setEditable(boolean b) {
				basicComposite.setEnabled(b);
				inputComposite.setEnabled(b);
			}
			public boolean isDirMode() {
				return dirMode;
			}
			public void setDirMode(boolean dirMode) {
				this.dirMode = dirMode;
			}
			

			
		}